using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SideShow.Scf.ObjectModel;
using System.Drawing.Imaging;
using SideShow.Contents;
using System.Globalization;
using SideShowClient;

namespace PocketPCSideShowClient
{
    public partial class ScfContentControl : ScfUserControl
    {
        public ScfContentControl(ScfNavigator nav) : base(nav)
        {
            InitializeComponent();
        }

        protected override void BindData()
        {
            base.BindData();
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            ScfContent cnt = (ScfContent)this.ScfObject;
            if (cnt.BackgroundImageId != 0)
            {
                Image bgImg = Navigator.GetImage(cnt.BackgroundImageId);
                DrawBackground(e.Graphics, cnt, bgImg);
            }
            int y = 2;
            Font f = new Font("Arial", 8, FontStyle.Regular);
            foreach (ScfObject obj in cnt.Children)
            {
                if (obj is ScfImage)
                {
                    ScfImage scfImg = (ScfImage)obj;
                    Image img = Navigator.GetImage(scfImg.Id);
                    int imgWidth = img.Width;
                    int imgHeight = img.Height;
                    Rectangle srcRect = new Rectangle(0, 0, imgWidth, imgHeight);
                    switch (scfImg.ImageFit)
                    {
                        case ScfImageFit.Auto:
                        case ScfImageFit.Native:
                            imgWidth = img.Width > this.ClientSize.Width ? this.ClientSize.Width : img.Width;
                            srcRect = new Rectangle(0, 0, imgWidth, imgHeight);
                            break;
                        case ScfImageFit.Screen:
                            imgWidth = this.ClientSize.Width;
                            imgHeight = this.ClientSize.Height;
                            break;
                        case ScfImageFit.Width:
                            imgWidth = this.ClientSize.Width;
                            break;
                    }
                    int x = 0;
                    if (imgWidth < this.ClientSize.Width)
                    {
                        switch (scfImg.Alignment)
                        {
                            case ScfAlignment.Center:
                                x = (this.ClientSize.Width - imgWidth) / 2;
                                break;
                            case ScfAlignment.Left:
                                x = 0;
                                break;
                            case ScfAlignment.Right:
                                x = this.ClientSize.Width - imgWidth;
                                break;
                        }
                    }
                    Rectangle dstRect = new Rectangle(x, y, imgWidth, imgHeight);
                    e.Graphics.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel);
                    y += imgHeight;
                }
                else if (obj is ScfText)
                {
                    ScfText scfText = (ScfText)obj;
                    StringFormat fmt = new StringFormat();
                    fmt.Alignment = StringAlignment.Near;
                    fmt.LineAlignment = scfText.Aligment == ScfAlignment.Left ? StringAlignment.Near : scfText.Aligment == ScfAlignment.Right ? StringAlignment.Far : StringAlignment.Center;
                    fmt.Alignment = fmt.LineAlignment;
                    if (!scfText.Wrap)
                    {
                        fmt.FormatFlags = StringFormatFlags.NoWrap;
                    }
                    SolidBrush b = new SolidBrush(Color.FromArgb(int.Parse(scfText.Color, NumberStyles.HexNumber)));
                    int x = 0;
                    string text = string.Empty;
                    foreach (ScfTextData td in scfText.Text)
                    {
                        text += td.LineBreak ? "\r\n" : td.Text;
                    }
                    SizeF ts = e.Graphics.MeasureString(text, f);
                    RectangleF rf = new RectangleF(x, y, this.ClientSize.Width - x, ts.Height);
                    e.Graphics.DrawString(text, f, b, rf, fmt);
                    y += (int)ts.Height;
                }
                else if (obj is ScfBreak)
                {
                    SizeF ts = e.Graphics.MeasureString("\r\n", f);
                    y += (int)ts.Height;
                }
                y += 1;
            }
            if (y > this.ClientSize.Height)
            {
                this.ClientSize = new Size(this.ClientSize.Width, y);
            }
            base.OnPaint(e);
        }

        private void DrawBackground(Graphics graphics, ScfContent cnt, Image bgImg)
        {
            Rectangle imgRect = new Rectangle(0, 0, bgImg.Width, bgImg.Height);
            switch (cnt.BackgroundFitMode)
            {
                case ContentBackgroundFitMode.Center:
                    Rectangle r = this.ClientRectangle;
                    if (r.Contains(imgRect))
                    {
                        int x = (r.Width - imgRect.Width) / 2;
                        int y = (r.Height - imgRect.Height) / 2;
                        graphics.DrawImage(bgImg, x, y);
                    }
                    else
                    {
                        graphics.DrawImage(bgImg, this.ClientRectangle, imgRect, GraphicsUnit.Pixel);
                    }
                    break;
                case ContentBackgroundFitMode.Scale:
                    graphics.DrawImage(bgImg, this.ClientRectangle, imgRect, GraphicsUnit.Pixel);
                    break;
                case ContentBackgroundFitMode.Tile:
                    TextureBrush tb = new TextureBrush(bgImg);
                    graphics.FillRectangle(tb, this.ClientRectangle);
                    break;
            }
        }
    }
}
